using IrisGenerator.Common.Tools;
using System;
using IrisGenerator.Common.DataTypes;
using System.Linq;
using System.ComponentModel;
using IrisGenerator.Common.Core.Rules;
using IrisGenerator.Framework.RuleEngine;
using IrisGenerator.Framework.Events;
namespace IrisGenerator.Common.Core
{
    public partial class Target : BindableObject<Target>, IRuleable<Target>, INotifyPropertyChanged
    {

        #region Properties

        public bool IsDirty { get; private set; }

        #endregion

        #region Events

        public event EventHandler<BaseEventArgs<Target>> Inserting;
        public event EventHandler<BaseEventArgs<Target>> Inserted;
        public event EventHandler<BaseEventArgs<Target>> Deleting;
        public event PropertyChangedEventHandler PropertyChanged;

        
        protected void OnDeleting(BaseEventArgs<Target> e)
        {
            if (Deleting != null)
                Deleting(this, e);
        }
        protected void OnInserting(BaseEventArgs<Target> e)
        {
            if (Inserting != null)
                Inserting(this, e);
        }
        protected void OnInserted(BaseEventArgs<Target> e)
        {
            if (Inserted != null)
                Inserted(this, e);
        }
        protected void OnPropertyChanged(string PropertyName)
        {
            this.IsDirty = true;

            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }

        #endregion

        #region ctor

        public Target():base()
        {
            this.Rules=new TargetRule(this);
        }

        #endregion

        #region IRuleable Members

        public RuleBase<Target> Rules { get; private set; }

        public TargetRule ObjectRules
        {
            get
            {
                return this.Rules as TargetRule;
            }
        }

        #endregion

        #region Overrides

        public override bool Delete()
        {
            return this.Delete(false);
        }

        #endregion

        public static Target SelectByCode(string code)
        {
            IrisGenerator.Dal.Target dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Target();
                System.Data.DataSet ds = dbo.TARGET_SelectByCode(code);
                Target obj = null;
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        obj = new Target();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static void InitEquivalences(Guid? TargetId)
        {
            IrisGenerator.Dal.Target dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Target();
                dbo.TARGET_InitEquivalences(TargetId);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
            
        }

        public static void UpdateEquivalences(Guid? TargetId)
        {

            MetatypeEquivalences.CleanLinks();

            MetatypeCollection col = Metatype.GetAll();
            MetatypeEquivalencesCollection equ_col = 
                MetatypeEquivalences.Select_METATYPE_EQUIVALENCES_By_TAR_ID(TargetId);

            foreach(Metatype metatype in col)
            {
                MetatypeEquivalences equ =
                    MetatypeEquivalences.Load(TargetId, metatype.MetId);
                if (equ == null)
                {
                    equ = new MetatypeEquivalences();
                    equ.TarId = TargetId;
                    equ.Metatype = metatype;
                    equ.Insert();
                }
            }

        }

        
    }
}
